﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using edowork.com.SQL2005;
using edowork.com.Model;

namespace WebInvoicingsys.system.sale
{
    public partial class customer_edit : System.Web.UI.Page
    {
        //前台输出提示信息变量
        public string alert_msg = "";

        string id = "", customername = "";

        /// <summary>
        /// 取得传入的参数
        /// </summary>
        /// <returns></returns>
        private bool GetParma()
        {
            try
            {
                id = Request.Params["id"].ToString();
                customername = Request.Params["customername"].ToString();
            }
            catch
            {
                //显示出错信息
                alert_msg = String.Format(Scripts.alert_warning, Errormsg.er_Parampost);
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
                return false;
            }

            return true;
        }

        private void bind()
        {
            Execute exec = new Execute(Session["connectionString"].ToString());

            string sql = "", error_msg = "";

            //构造SQL语句
            sql = @"select a.customerid, a.customername, d.cityid, a.address, a.contact, a.mobile, a.email, a.phone, a.fax, a.qq, b.settlename, c.creditname, a.sysdate, a.settleid, a.creditid
                      from customer a, settle b, credit c, v_city d
                       where a.settleid=b.settleid
                        and  a.creditid=c.creditid
                        and  a.customerid='{0}'";


            sql = String.Format(sql, id);

            DataSet ds = exec.ExecSQLQueryDataSet(sql, ref error_msg);

            if ((ds == null) || (ds.Tables.Count <= 0) || (ds.Tables[0].Rows.Count <= 0))
            {
                alert_msg = String.Format(Scripts.alert_warning, error_msg);
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
                return;
            }

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                txt_customername.Text = dr["customername"].ToString();
                txt_address.Text = dr["address"].ToString();
                txt_contact.Text = dr["contact"].ToString();
                txt_mobile.Text = dr["mobile"].ToString();
                txt_email.Text = dr["email"].ToString();
                txt_phone.Text = dr["phone"].ToString();
                txt_fax.Text = dr["fax"].ToString();
                txt_qq.Text = dr["qq"].ToString();

                uc_from_city.SetByValue(dr["cityid"].ToString());
                uc_from_settle.SetByValue(dr["settleid"].ToString());
                uc_from_credit.SetByValue(dr["creditid"].ToString());
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ;
            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetParma();

                bind();
            }
        }

        //保存
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (GetParma() == false)
            {
                return;
            }

            alert_msg = "";

            edowork.com.Model.customerInfo customerInfo = new customerInfo();

            customerInfo.Customername = txt_customername.Text.Trim();
            customerInfo.Cityid = uc_from_city.getValue();
            customerInfo.Address = txt_address.Text.Trim();
            customerInfo.Contact = txt_contact.Text.Trim();
            customerInfo.Mobile = txt_mobile.Text.Trim();
            customerInfo.Email = txt_email.Text.Trim();
            customerInfo.Phone = txt_phone.Text.Trim();
            customerInfo.Fax = txt_fax.Text.Trim();
            customerInfo.Qq = txt_qq.Text.Trim();
            customerInfo.Settleid = uc_from_settle.getValue();
            customerInfo.Creditid = uc_from_credit.getValue();

            if (String.IsNullOrEmpty(customerInfo.Customername))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请输入客户名称");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                txt_customername.Focus();

                return;
            }

            if (String.IsNullOrEmpty(customerInfo.Address))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请输入详细地址");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                txt_address.Focus();

                return;
            }

            if (String.IsNullOrEmpty(customerInfo.Contact))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请输入联系人");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                txt_contact.Focus();

                return;
            }

            if (String.IsNullOrEmpty(customerInfo.Settleid))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请选择结算方式");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                return;
            }

            if (String.IsNullOrEmpty(customerInfo.Creditid))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请选择信用等级");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                return;
            }

            //保存数据入数据库
            using (edowork.com.SQL2005.TranUtil tranutil = new TranUtil(Session["connectionString"].ToString()))
            {
                System.Data.SqlClient.SqlTransaction tran = null;

                try
                {
                    tran = tranutil.GetSqlTransaction();
                }
                catch (Exception er)
                {
                    alert_msg = String.Format(Scripts.alert_warning, er.Message);

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                    return;
                }

                edowork.com.SQL2005.Execute exec = new Execute(Session["connectionString"].ToString());

                string sql = "";
                int result = -1;

                sql = @"update customer set customername='{1}', cityid='{2}', address='{3}', contact='{4}', mobile='{5}', email='{6}', phone='{7}', fax='{8}', qq='{9}', settleid='{10}', creditid='{11}' where customerid='{0}'";

                sql = String.Format(sql, id, customerInfo.Customername, customerInfo.Cityid, customerInfo.Address, customerInfo.Contact, customerInfo.Mobile, customerInfo.Email,
                                  customerInfo.Phone, customerInfo.Fax, customerInfo.Qq, customerInfo.Settleid, customerInfo.Creditid);

                result = exec.ExecSQLQueryInt(sql, ref alert_msg, tran);

                if (result <= 0)
                {
                    tran.Rollback();
                    tranutil.CloseConnection();

                    alert_msg = String.Format(Scripts.alert_warning, alert_msg);

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                    return;
                }

                tran.Commit();
                tranutil.CloseConnection();
            }

            alert_msg = String.Format(Scripts.alert_success, Errormsg.success_Save);

            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
        }

        //返回
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            GetParma();

            string url = String.Format("/system/sale/customer_list.aspx?customername={0}", customername);

            Response.Redirect(url);
        }
    }
}